#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: IcySun
# 脚本功能：爆破网站的二级域名

from Queue import Queue
import threading,sys
import dns.resolver


def use():
    print '#' * 50
    print '\t python subDoman.py xxx.com'
    print '\t\t\t Code By: IcySun'
    print '#' * 50

def subDoman(domain):
    global dname
    dname = sys.argv[1]
    try:
        ns = dns.resolver.query(domain)
        for i in ns.response.answer:
            for j in i.items:
                if j.to_text().count('.') == 3:
                    print domain + '\t\t' + j.to_text()
                    with open(dname + '.txt','a+') as name:
                        name.write(domain + '\t\t' + j.to_text() + '\n')
    except Exception, e:
        pass

class MyThread(threading.Thread): 
    def __init__(self): 
        threading.Thread.__init__(self) 
    def run(self): 
        global queue 
        while not queue.empty(): 
            domain = queue.get() 
            subDoman(domain)

def main():
    global domain,queue
    queue = Queue()
    dname = sys.argv[1]
    namelist = open('yixia.txt','r')
    for name in namelist.readlines():
        domain = name.strip() + '.' + dname
        queue.put(domain)

    for i in range(10): 
        c = MyThread() 
        c.start()

if __name__ == '__main__':
    if len(sys.argv) != 2:
        use()
    else:
        main()